<?php

/**
 * @author Nguyen Thai Binh
 */
class File_upload extends CI_Controller {

  protected $class;
  public $base_lib;

  public function __construct() {
    parent::__construct();
    $this->class = $this->router->fetch_class();
    $this->base_lib = new Base_lib();
    set_userdata('currentURL', current_url());
  }

  public function index() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $dir = $this->setting_model->item('upload_path') . '/' . $path;
    if (is_dir($dir)) {
      $dh = opendir($dir);
      $folders = array();
      $files = array();
      while (false !== ($filename = readdir($dh))) {
        if ($filename != '.' && $filename != '..') {
          if (is_dir($this->setting_model->item('upload_path') . '/' . $path . '/' . $filename)) {
            $folders[] = $filename;
          } else if (is_file($this->setting_model->item('upload_path') . '/' . $path . '/' . $filename)) {
            $files[] = $filename;
          }
        }
      }
      $this->load->view('admin/' . $this->class . '/index', array(
          'folders' => $folders,
          'files' => $files,
          'dir' => $path,
          'tree_folders' => ''
      ));
    } else {
      set_flash_error($this->lang->line('error.folder.not.found', $path));
      redirect('admin/index');
    }
  }

  public function view_file() {
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $path = $this->setting_model->item('upload_path') . '/' . $path;
    if (is_file($path)) {
      echo '<img src="' . $this->base_lib->get_img_from_path($path) . '" alt="' . $this->lang->line('error.file.not.found', $path) . '">';
    } else {
      echo $this->lang->line('error.file.not.found', $path);
    }
  }

  public function edit_file() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $path = $this->setting_model->item('upload_path') . '/' . $path;
    if (is_file($path)) {
      $this->load->view('admin/' . $this->class . '/edit_file', array(
          'path' => $_REQUEST['path'],
          'content' => file_get_contents($path)
      ));
    } else {
      echo set_flash_error($this->lang->line('error.file.not.found', $path));
    }
  }

  public function save_file() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $path = $this->setting_model->item('upload_path') . '/' . $path;
    $content = empty($_REQUEST['content']) ? '' : $_REQUEST['content'];
    if (is_file($path)) {
      $f = fopen($path, "w");
      fwrite($f, $content);
      fclose($f);
      echo json_encode(array('status' => 1, 'message' => $this->lang->line('message.file.saved', $path)));
    } else {
      echo json_encode(array('status' => 0, 'message' => $this->lang->line('error.file.not.found', $path)));
    }
  }

  public function create_folder() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    if (is_dir($this->setting_model->item('upload_path') . '/' . $path)) {
      $this->load->view('admin/' . $this->class . '/create_folder', array(
          'path' => $_REQUEST['path']
      ));
    } else {
      echo $this->lang->line('error.folder.not.found', $path);
    }
  }

  public function save_folder() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    if (is_dir($this->setting_model->item('upload_path') . '/' . $path)) {
      $folder_name = empty($_REQUEST['folder_name']) ? '' : $_REQUEST['folder_name'];
      @mkdir($this->setting_model->item('upload_path') . '/' . $path . '/' . $folder_name);
      if (is_dir($this->setting_model->item('upload_path') . '/' . $path . '/' . $folder_name)) {
        echo json_encode(array('status' => 1, 'message' => $this->lang->line('message.folder.created', $folder_name)));
      } else {
        echo json_encode(array('status' => 0, 'message' => $this->lang->line('error.folder.cannot.create', $folder_name)));
      }
    } else {
      echo json_encode(array('status' => 0, 'message' => $this->lang->line('error.folder.not.found', $path)));
    }
  }

  public function upload() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    if (is_dir($this->setting_model->item('upload_path') . '/' . $path)) {
      $this->load->view('admin/' . $this->class . '/upload', array(
          'path' => $_REQUEST['path']
      ));
    } else {
      echo $this->lang->line('error.folder.not.found', $path);
    }
  }

  public function upload_process() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    if (is_dir($this->setting_model->item('upload_path') . '/' . $path)) {
      if (count($_FILES) < 1) {
        set_flash_error($this->lang->line('error.upload.fail.no.file'));
      }
      $file = $_FILES['file'];
      include APPPATH . 'libraries/File.php';
      $file = new File($file);
      if ($file->type == '') {
        set_flash_error($this->lang->line('error.upload.fail.no.file'));
      }
      $max_size = -1;
      if ($max_size > 0) {
        if ($file->size() > $max_size) {
          set_flash_error($this->lang->line('error.upload.fail.big'));
        }
      }
      if (str_startsWith($path, './')) {
        $path = substr($path, 2);
      }
      $result = $file->copyFromTemp($this->setting_model->item('upload_path') . '/' . $path, true);
      $this->load->helper('file');
      if ($result) {
        set_flash_message($this->lang->line('message.upload.success'));
      }
    } else {
      set_flash_error($this->lang->line('error.folder.not.found', $path));
    }
    redirect('admin/'.$this->class . '/index?path=' . $path);
  }

  public function delete_folder() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $temp = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $temp = str_replace('\\', '', $temp);
    $path = $this->setting_model->item('upload_path') . '/' . $temp;
    if (is_file($path)) {
      @unlink($path);
      redirect('admin/'.$this->class . '/index?path=' . dirname($temp));
    } else if (is_dir($path)) {
      deletedir($path);
      redirect('admin/'.$this->class . '/index?path=' . dirname($temp));
    } else {
      echo set_flash_error($this->lang->line('error.file.not.found', $path));
      redirect('admin/'.$this->class . '/index?path=' . dirname($path));
    }
  }

  public function download() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $path = $this->setting_model->item('upload_path') . '/' . $path;
    if (is_file($path)) {
      $mm_type = "application/binary";
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-Type: " . $mm_type);
      header("Content-Length: " . filesize($path));
      $pathinfo = pathinfo($path);
      header('Content-Disposition: attachment; filename="' . $pathinfo['basename'] . '"');
      header("Content-Transfer-Encoding: binary\n");
      readfile($path);
    } else {
      show_404();
    }
  }

  public function get_img_from_path() {
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $this->base_lib->get_img_from_path($this->setting_model->item('upload_path') . $path);
  }

  public function ajax_file() {
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $path = empty($_REQUEST['path']) ? '' : $_REQUEST['path'];
    $dir = $this->setting_model->item('upload_path') . '/' . $path;
    if (is_dir($dir)) {
      $dh = opendir($dir);
      $folders = array();
      $files = array();
      while (false !== ($filename = readdir($dh))) {
        if ($filename != '.' && $filename != '..') {
          if (is_dir($this->setting_model->item('upload_path') . '/' . $path . '/' . $filename)) {
            $folders[] = $filename;
          } else if (is_file($this->setting_model->item('upload_path') . '/' . $path . '/' . $filename)) {
            $files[] = $filename;
          }
        }
      }
      $this->load->view('admin/' . $this->class . '/ajax_file', array(
          'folders' => $folders,
          'files' => $files,
          'dir' => $path,
          'tree_folders' => ''
      ));
    } else {
      echo set_flash_error($this->lang->line('error.folder.not.found', $path));
    }
  }

}